/**
 * @file 页面语言、换肤及左侧导航栏相关脚本
 */

$(function ($) {
  const lang = location.pathname.split('/')[1];
  var evaluateParams = {
    name: '',
    path: '',
    lang: '',
    version: '',
    stars: 0
  };
  evaluateParams.lang = lang;
  if (lang === 'zh') {
    $('#lang>.option>a').eq(0).addClass('action');
    $('#lang>.option>a').eq(1).removeClass('action');
    $('#lang>.option>a').eq(2).addClass('action');
    $('#lang>.option>a').eq(3).removeClass('action');
  } else {
    $('#lang>.option>a').eq(0).removeClass('action');
    $('#lang>.option>a').eq(1).addClass('action');
    $('#lang>.option>a').eq(2).removeClass('action');
    $('#lang>.option>a').eq(3).addClass('action');
  }
  // 切换语言
  $('.h5-right>.icon-lang,#lang').click(function (e) {
    $(this).find('.option').show();
    $(document).one('click', function () {
      $(this).find('.option').hide();
    });
    e.stopPropagation();
  });
  $('.right-content>#lang>.option>a,.h5-left>#lang>.option>a').click(
    function () {
      const url = location.href;
      if (url.search('/zh/') === -1) {
        if ($(this).attr('ref') === 'zh') {
          window.location.replace(url.replace('/en/', '/zh/'));
        }
      } else {
        if ($(this).attr('ref') === 'en') {
          window.location.replace(url.replace('/zh/', '/en/'));
        }
      }
    }
  );
});
// 左侧导航栏相关
function getTreeLink() {
  setTimeout(function () {
    let openEle = $('#docstreeview .jstree-container-ul').find('.jstree-open');
    for (let i = 0; i < openEle.length; i++) {
      if (i < openEle.length - 1) {
        let span = '<i></i>';
        $('.page-title')
          .append(
            $('#docstreeview .jstree-container-ul')
              .find('.jstree-open')
              .eq(i)
              .find('a')
              .first()
              .clone()
          )
          .append(span);
      } else {
        let text = $('#docstreeview .jstree-container-ul')
          .find('.jstree-open')
          .eq(i)
          .find('a')
          .first()
          .text();
        let span = '<span>' + text + '</span>';
        $('.page-title').append(span);
      }
    }
  }, 500);
}
getTreeLink();

setTimeout(function () {
  $('#docstreeview .jstree-container-ul a.active')
    .closest('.jstree-open')
    .children('.jstree-anchor')
    .find('p a')
    .css({
      color: '#FEB32A'
    });
}, 100);

// 获取 cookie
function getCustomCookie(key) {
  const name = `${encodeURIComponent(key)}=`;
  const decodedCookies = decodeURIComponent(document.cookie);
  const cookies = decodedCookies.split('; ');
  for (let cookie of cookies) {
    if (cookie.startsWith(name)) {
      return cookie.substring(name.length);
    }
  }

  return null;
}
// 设置 cookie
function setCustomCookie(key, value, day = 1, domain = location.hostname) {
  const expires = new Date();
  expires.setTime(expires.getTime() + day * 24 * 60 * 60 * 1000);
  const cookie = `${encodeURIComponent(key)}=${encodeURIComponent(
    value
  )}; expires=${expires.toUTCString()}; path=/; domain=${domain}`;
  document.cookie = cookie;
}


// 皮肤主题切换
(function () {
  const domain = '.openlookeng.io';
  const APPEARANCE_KEY = 'openLooKeng-theme-appearance';
  const themeStyle = getCustomCookie(APPEARANCE_KEY);
  const html = $('html');
  if (!themeStyle) {
    $('.theme-change i').removeClass('light dark').addClass('light');
    $('.title-h2 .icon-help').removeClass('dark');
    $('.nav-menu a .h5-logo').removeClass('dark');
    html.removeClass('light dark').addClass('light'); 
  } else {
    $('.theme-change i').removeClass('light dark').addClass(themeStyle);
    $('.title-h2 .icon-help').addClass(themeStyle);
    $('.nav-menu a .h5-logo').addClass(themeStyle); 
  }
  $('.theme-change i').click(function () {
    if ($(this).hasClass('light')) {
      $('.title-h2 .icon-help').addClass('dark');
      $('.nav-menu a .h5-logo').addClass('dark');
      $(this).addClass('dark').removeClass('light');
      setCustomCookie(APPEARANCE_KEY, 'dark', 180, domain);
      html.addClass('dark').removeClass('light');
    } else {
      $('.nav-menu a .h5-logo').removeClass('dark');
      $('.title-h2 .icon-help').removeClass('dark');
      $(this).addClass('light').removeClass('dark');
      setCustomCookie(APPEARANCE_KEY, 'light', 180, domain);
      html.addClass('light').removeClass('dark');
    }
  });
})()

window.onload = function () {
  const lang = location.pathname.split('/')[1];
  if (lang === 'zh') {
    let content = document.querySelector('#markdown');
    content.onclick = function (ev) {
      var ev2 = ev || window.event;
      ev2.cancelBubble = true;
    };
  }
};
